<?php


class AuthController extends Zend_Controller_Action
{
	/**
	 * Die DefaultAction dieses Controllers.
	 *
	 */
    public function loginAction()
    {

		// Ein anderes Layout Skript in dieser Action verwenden:
        $this->_helper->layout->setLayout('login');
		$this->_helper->viewRenderer->setNoRender();

    	$request = $this->getRequest();
		$username = $request->getPost('username');
		$password = $request->getPost('password');
		if(!empty($username) && !empty($password)) {

			$adapter = new WSB_Auth_Adapter_Backend($username, md5($password));
			$auth = WSB_Auth_Backend::getInstance();
			$result = $auth->authenticate($adapter);

			// Login ist erfolgreich
			if($result->isValid()) {

				WSB_Log::info('User logged in');

				$beuser = $result->getIdentity();
				$acl = $beuser->getAcl();

				// Redirect zum Benutzerspezifischen Admin Default Controller
				$adminDefaultControllerName = $beuser->getDefaultController();
				if($acl->isAllowed(WSB_ACL_ROLE_ADMIN) || $acl->isAllowed(WSB_ACL_ROLE_STAFF, null, $adminDefaultControllerName)) {
					header('Location: http://' . WSB_HOST . WSB_ADMIN_URL . $adminDefaultControllerName);
				} else {
					header('Location: ' . $_SERVER['REQUEST_URI']);
				}
				exit;
			}
		}
    }

    public function logoutAction()
    {
		$this->_helper->viewRenderer->setNoRender();

        WSB_Log::info('User logged out');
        WSB_Auth_Backend::getInstance()->clearIdentity();
        Zend_Session::destroy();
        header('Location: http://' . WSB_HOST . WSB_ADMIN_URL);
    }

}
